const express = require('express');
const svgCaptcha = require('svg-captcha');
const router = express.Router();
router.get('/', (req, res, next) => {
  const ip = req.ip;
  const codeMap = global.codeMap;
  if (codeMap[ip] && ((+new Date()) - codeMap[ip].time <= global.limitTime)) {
    return res.status(400).send('请求频繁，稍后再试！');
  }
  next();
}, (req, res) => {
  const ip = req.ip;
  const codeMap = global.codeMap;
  const captcha = svgCaptcha.create();
  codeMap[ip] = {
    time: +new Date(),
    value: captcha.text.toLocaleLowerCase()
  };
  res.type('svg');
  res.status(200).send(captcha.data);
});
module.exports = router;